﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using nvn.Core.MVP;
using Hutech.CRM.Library.Views;
using Hutech.CRM.Library.Models.Entities;
using Hutech.CRM.Library.Utilities;
using nvn.Library.Patterns;
using System.Drawing;
using Hutech.Authentication.Library.Web.Framework;

namespace Hutech.CRM.Web.Controls.ManageCustomers
{
    public partial class ListCustomersControl : System.Web.UI.UserControl, IListCustomerView
    {
        #region MEMBER VARIABLES
        protected string EditPropertiesImage = "~/Data/SiteImages/" + WebConfigSettings.EditPropertiesImage;
        protected string DeleteLinkImage = "~/Data/SiteImages/" + WebConfigSettings.DeleteLinkImage;
        public int _PageSize;
        private int _TotalPages = 1;
        #endregion

        #region Cac su kien uy thac
        public event EventHandler ReloadData;
        protected void onReloadData()
        {
            if (ReloadData != null)
            {
                ReloadData(this, new EventArgs());
            }
        }
        #endregion

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.InitView();
            pgrItems.Command += new System.Web.UI.WebControls.CommandEventHandler(pgrItems_Command);
            DropDownListPageNumber.SelectedIndexChanged += new EventHandler(DropDownListPageNumber_SelectedIndexChanged);
            this.DataListCustomers.ItemCommand += new DataListCommandEventHandler(DataListCustomers_ItemCommand);
            this.DataListCustomers.ItemDataBound += new DataListItemEventHandler(DataListCustomers_ItemDataBound);


        }

        void DataListCustomers_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.EditItem)
            {
                //DropDownList DropDownListGroupLevelCustomer = (DropDownList)e.Item.FindControl("DropDownListGroupLevelCustomer");
                //this.OnListCriterionCustomer_ItemDataBound("LoadGroupLevel");// load dropdownlist

                //DropDownListGroupLevelCustomer.DataSource = GroupLevelCustomers;
                //DropDownListGroupLevelCustomer.DataValueField = "T";
                //DropDownListGroupLevelCustomer.DataTextField = "V";
                //DropDownListGroupLevelCustomer.DataBind();

                ////dung co ngu ngoc nhu cho nay nua :D
                //DropDownListGroupLevelCustomer.SelectedValue = ((HiddenField)e.Item.FindControl("HiddenFieldKeyGroupLevel")).Value.ToString();
            }



            //else
            //{
            //    bool IsActive = bool.Parse(((HiddenField)e.Item.FindControl("HiddenFieldIsActive")).Value.ToString());
            //    //Set color
            //    Panel PanelLine = (Panel)e.Item.FindControl("PanelLine");
            //    if (IsActive)
            //        PanelLine.ForeColor = Color.Blue;
            //    else
            //        PanelLine.ForeColor = Color.Black;
            //    //if (e.Row.RowIndex >= 0)
            //    //{
            //    //    e.Row.Attributes.Add("onMouseOver", "this.style.backgroundColor='#bde2fd'");
            //    //    if (e.Row.RowIndex % 2 == 0)
            //    //        e.Row.Attributes.Add("onMouseOut", "this.style.backgroundColor=''");
            //    //    else
            //    //        e.Row.Attributes.Add("onMouseOut", "this.style.backgroundColor=''");
            //    //}tr.Attributes.Add("style", "cursor:hand;");
            //}
        }
        void DataListCustomers_ItemCommand(object source, DataListCommandEventArgs e)
        {
            AccountCustomer item = new AccountCustomer();

            //Search value control
            if (e.CommandName.Equals(ConstantVariable.COMMAND_APPLY))
            {
                item.HoTen = ((TextBox)e.Item.FindControl("TextBoxName")).Text;
                item.DienThoai = ((TextBox)e.Item.FindControl("TextBoxPhoneNumber")).Text;
                item.DiaChi = ((TextBox)e.Item.FindControl("TextBoxAddress")).Text;
            }
            //if (e.CommandName.Equals(ConstantVariable.COMMAND_ENABLE))
            //{
            //    item.IsActive = ((CheckBox)e.Item.FindControl("CheckBoxIsActive")).Checked;
            //}

            this.OnAccountCustomer_ItemCommand(
                e.CommandName.ToString(),
                DataListCustomers.DataKeys[e.Item.ItemIndex].ToString(),
                e.Item.ItemIndex,
                item);
        }
        void DropDownListPageNumber_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                _PageSize = Convert.ToInt32(DropDownListPageNumber.SelectedValue.ToString());
                onReloadData();
                //OnSelectedIndexChanged_EventArgs(_PageSize);
            }
            catch (Exception)
            {
                //WebUtils.SetupRedirect(this, Request.RawUrl);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                OnFristLoadEventHandler();
        }

        #region Event
        void pgrItems_Command(object sender, System.Web.UI.WebControls.CommandEventArgs e)
        {
            PageNumber = Convert.ToInt32(e.CommandArgument);
            pgrItems.CurrentIndex = PageNumber;
            onReloadData();
        }
        #endregion

        #region Implement from View class
        #region Property
        #region Phan trang
        public int PageNumber
        {
            get
            {
                if (ViewState["pageNumber"] != null)
                {
                    try { return Convert.ToInt32(ViewState["pageNumber"].ToString()); }
                    catch { return 1; } // add
                }
                return 1;
            }
            set
            {
                ViewState["pageNumber"] = value;
            }
        }
        public int PageSize
        {
            get
            {
                try
                {
                    return Convert.ToInt32(DropDownListPageNumber.SelectedValue);
                }
                catch { return 15; } // add
            }
            set
            {
                this._PageSize = value;
            }
        }
        public int TotalPages
        {
            set { this._TotalPages = value; }
        }
        #endregion
        public List<AccountCustomer> AccountCustomers
        {
            set
            {
                DataListCustomers.DataSource = value;
                DataListCustomers.DataBind();
                //phan trang
                if (this._TotalPages > 1)
                {
                    pgrItems.Visible = true;
                    pgrItems.ShowFirstLast = true;
                    pgrItems.PageSize = this._PageSize;
                    pgrItems.PageCount = this._TotalPages;
                }
                else
                {
                    pgrItems.Visible = false;
                }
            }
        }
        public List<TowTypeParameters<string, string>> PapeNumbers
        {
            set
            {
                DropDownListPageNumber.Items.Clear();
                DropDownListPageNumber.DataSource = value;
                DropDownListPageNumber.DataTextField = "T";
                DropDownListPageNumber.DataValueField = "V";
                DropDownListPageNumber.DataBind();

                //if (_PageSize != null)
                //    DropDownListPageNumber.SelectedValue = _PageSize.ToString();
                //else
                //    DropDownListPageNumber.SelectedIndex = -1;
            }
        }
        public Guid DataTranfer
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        public bool Refresh
        {
            set { if (value) onReloadData(); }
        }
        public string ErrorMessage
        {
            set { throw new NotImplementedException(); }
        }
        public int EditItemIndex
        {
            set { DataListCustomers.EditItemIndex = value; }
        }
        #endregion

        #region Event
        public event EventHandler<EventArgs> FristLoadEventHandler;
        private void OnFristLoadEventHandler()
        {
            if (FristLoadEventHandler != null)
                FristLoadEventHandler(this, new EventArgs());
        }
        public event EventHandler<OneParameterEventAgrs<int>> SelectedIndexChanged_EventArgs;
        private void OnSelectedIndexChanged_EventArgs(int parameterValue)
        {
            if (SelectedIndexChanged_EventArgs != null)
                SelectedIndexChanged_EventArgs(this, new OneParameterEventAgrs<int>(parameterValue));
        }
        public event EventHandler<ItemCommandArgs<AccountCustomer>> AccountCustomer_ItemCommand;
        private void OnAccountCustomer_ItemCommand(string parameterName, string parameterValue, int itemIndex, AccountCustomer myType)
        {
            if (AccountCustomer_ItemCommand != null)
                AccountCustomer_ItemCommand(this, new ItemCommandArgs<AccountCustomer>(parameterName, parameterValue, itemIndex, myType));
        }
        public event EventHandler<ItemDataBoundArgs> AccountCustomer_ItemDataBound;
        private void OnAccountCustomer_ItemDataBound(string parameterName)
        {
            if (AccountCustomer_ItemDataBound != null)
                AccountCustomer_ItemDataBound(this, new ItemDataBoundArgs(parameterName));
        }
        public event EventHandler<OneParameterEventAgrs<SearchEntity>> Search_click;
        private void OnSearch_click(SearchEntity parameterValue)
        {
            if (Search_click != null)
                Search_click(this, new OneParameterEventAgrs<SearchEntity>(parameterValue));
        }
        #endregion
        #endregion

        //public int PagesValueSelected
        //{
        //    get
        //    {
        //        try
        //        {
        //            return Convert.ToInt32(DropDownListPageNumber.SelectedValue);
        //        }
        //        catch (Exception)
        //        {
        //            return -1;
        //        }
        //    }
        //    set
        //    {
        //        DropDownListPageNumber.SelectedValue = value.ToString();
        //    }
        //}

        #region Publish methods
        internal void Show(SearchEntity searchEntity)
        {
            OnSearch_click(searchEntity);
        }
        #endregion


       
    }
}